package com.jcj.house;

import android.view.Display;
import com.jcj.framework.Game;
import com.jcj.framework.Input.TouchEvent;
import com.jcj.framework.gl.Camera2D;
import com.jcj.framework.gl.SpriteBatcher;
import com.jcj.framework.impl.GLScreen;
import com.jcj.framework.math.OverlapTester;
import com.jcj.framework.math.Rectangle;
import com.jcj.framework.math.Vector2;
import com.jcj.house.Assets;
import com.jcj.house.Settings;

import java.util.List;
import javax.microedition.khronos.opengles.GL10;

public class MainMenuScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle soundBounds;
    Rectangle survivalBounds;
    Rectangle playBounds;
    Rectangle highscoresBounds;
    Rectangle helpBounds;
    Vector2 touchPoint;
    Display display;
    int width;
    int height;
    
    public MainMenuScreen(Game game) {
        super(game);
        guiCam = new Camera2D(glGraphics, 320, 480);
        batcher = new SpriteBatcher(glGraphics, 100);
        soundBounds = new Rectangle(0, 0, 64, 64);
        playBounds = new Rectangle(33,480-258-54 , 119, 53);
        survivalBounds = new Rectangle(31,480-324-54,120,54);
        helpBounds = new Rectangle(106, 480-399-42, 36, 42);
        touchPoint = new Vector2();
       
    }
    
    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        int len = touchEvents.size();
        for (int i = 0; i < len; i ++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);

                if (OverlapTester.pointInRectangle(playBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    game.setScreen(new PantallaJuego(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(survivalBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    //game.setScreen(new HighscoreScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    game.setScreen(new InfoScreen(game));
                    return;
                }

                if (OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
                    //Assets.playSound(Assets.clickSound);
                    Settings.soundEnabled = !Settings.soundEnabled;
                    //if (Settings.soundEnabled)
                    //    Assets.music.play();
                    //else
                    //    Assets.music.pause();
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();

        gl.glEnable(GL10.GL_TEXTURE_2D);
        batcher.beginBatch(Assets.inicio);
        batcher.drawSprite(160, 240, 320, 480, Assets.inicioRegion);
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.items);
        batcher.drawSprite(22, 48, 43, 96, Settings.soundEnabled ? Assets.soundOn : Assets.soundOff);
        batcher.endBatch();

        gl.glDisable(GL10.GL_BLEND);
    }

    @Override
    public void pause() {
        Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        
    }
}
